Accusoft.OCRXpress.nodejs
Tutorial: Create Your First Project

In this step-by-step tutorial we’ll build a complete application to recognize text in an image and output all the individual words and their locations to the terminal.

Before running this tutorial a license for OCRXpress for Node.js should already be installed.

If you have run the setup script in the install folder then you should already have an evaluation license. If not, then see Registering Evaluation Licenses to install a license.

For this tutorial, you can use your favorite text editor (vim, gedit, emacs, etc.).

  1. Create a new file titled ocrDemo.js
  2. Import the ocrxpress module (which includes the English language package):
    Copy Code
    var ocrx = require('ocr'); 
  3. Add some code to display a message that the sample is starting:
    Copy Code
    console.log('\n....................... Begin Tutorial .......................\n');
  4. Create the parameters to use for recognition - namely, the input image and the language to recognize:
    Copy Code
    var params = {
        input: '/path/to/image/file.bmp',
        language: 'english'
    };
  5. You will need to modify the "input" image path to an image on your local system.
    • Simply replace the text "/path/to/image/file.bmp/" with an image on your local system. You can use the sample images included under node_modules/ocr/samples/images/
  6. Now you can create a function to be called after recognition has been performed:
    Copy Code
    function afterOCR (err, doc) {
        if (err) {
            console.log('There was an error processing this image:');
            console.log('\t' + err);
            return;
        }
        // Output a message letting the user know we were successful.
        console.log('Successfully finished recognizing the image');
        // Create an array with just the words.
        var wordArray = doc.getWords().map(function(element) {
            var obj = {};
            obj[element.text] = element.area;
            return obj;
        });
        // Display the words.
        console.log('These are the words in your images:');
        console.log(wordArray);
    }
  7. Now you are ready to perform recognition:
    Copy Code
    // Perform recognition.
    ocrx.recognize(params, afterOCR);
    console.log('The words in your image are automatically being detected');
  8. That's it, now go to the console and run your application to see all the words in an image:
    Copy Code
    $ node ocrDemo.js
       

 

 

 


©2016. Accusoft Corporation. All Rights Reserved.

Send Feedback